package com.chunuo.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chunuo.commonutils.JwtUtils;
import com.chunuo.commonutils.R;
import com.chunuo.commonutils.ordervo.CourseWebVoOrder;
import com.chunuo.eduservice.entity.EduCourse;
import com.chunuo.eduservice.entity.frontvo.CourseFrontVo;
import com.chunuo.eduservice.entity.frontvo.CourseWebVo;
import com.chunuo.eduservice.entity.vo.EduChapterVo;
import com.chunuo.eduservice.openfeign.OrderOpenFeign;
import com.chunuo.eduservice.service.EduChapterService;
import com.chunuo.eduservice.service.EduCourseService;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-07-25 14:12
 */
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrderOpenFeign orderOpenFeign;

    //分页查询教师
    @PostMapping("/pageQeryCourse/{page}/{limit}")
    public R pageQeryCourse(@PathVariable("page")Long page, @PathVariable("limit")Long limit, @RequestBody CourseFrontVo courseFront){
        Map<String, Object> map = eduCourseService.pageQueryCourse(new Page<EduCourse>(page,limit),courseFront);
        return R.ok().data(map);
    }


    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<EduChapterVo> chapterVideoList = eduChapterService.findDescription(courseId);

        //是否购买了课程
        boolean isBuy = (boolean) orderOpenFeign.getIsBuy(courseId, JwtUtils.getMemberIdByJwtToken(request)).getData().get("isBuy");

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("buy",isBuy);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public CourseWebVoOrder getCourseInfo(@PathVariable("courseId")String courseId){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        CourseWebVoOrder order = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,order);
        return order;
    }


}
